#include "ParticleSensorObserver.h"

#include "ParticleContext.h"
#include "DescriptorParticle.h"

#include <StatusComponent.h>

using namespace ParticleEngine;

const DescriptorParticle ParticleSensorObserver::Descriptor
(
"SensorObserver",
[](ParticleContext& NewContext) { return new ParticleSensorObserver(NewContext); }
);

ParticleSensorObserver::ParticleSensorObserver(ParticleContext& NewContext) :
ParticleBase(NewContext, &Descriptor),
PropertyAdhesions(0.15f),
Heat(0.5f)
{
	this->Adhesions = dynamic_cast<PropertyAdhesions*>(this);
	this->Adhesions->ParticleCast = dynamic_cast<ParticleBase*>(this);
}

ParticleSensorObserver::~ParticleSensorObserver()
{
}


void ParticleSensorObserver::Sole()//einzelne aktualisierzung
{
	this->SoleStandard();
	this->DirectionTracer.Sole(this);

	if (Context.Observer.FocusParticle != nullptr && this->DirectionTracer.DirectorFound > 0)
	{
		const Vec<2, float> VecSpace = Vec<2, float>(Context.Observer.FocusParticle->Pos - this->Pos).Normalize();
		const Vec<2, float> VecDirection = DirectionTracer.Direction;

		const float y = (VecDirection.X * VecSpace.Y) - (VecSpace.X * VecDirection.Y);
		const float x = (VecDirection.X * VecSpace.X) + (VecDirection.Y * VecSpace.Y);
		const float Angle = atan2(y, x);

		if (Angle > 0.f)
		{
			this->Signal0.Current = SignalStatus::Active;
			this->Signal1.Current = SignalStatus::Inactive;
		}
		else
		{
			this->Signal0.Current = SignalStatus::Inactive;
			this->Signal1.Current = SignalStatus::Active;
		}
	}
	else
	{
		this->Signal0.Current = SignalStatus::Inactive;
		this->Signal1.Current = SignalStatus::Inactive;
	}

	this->Heat.Sole();
	//this->Signal0.Sole();
	//this->Signal1.Sole();
}
void ParticleSensorObserver::InteractionIndividual(ParticleBase*const Other, Vec<2, float>& Space, const float& DistanceSquared)
{
	this->InteractionKinetics(Other, Space, DistanceSquared);

	this->Heat.Interaction(Other->HasHeat());

	this->Signal0.Interaction(Other->HasSignal0());
	this->Signal1.Interaction(Other->HasSignal1());
}
ColorRGBA<unsigned char> ParticleSensorObserver::GetColor()
{
	if (Signal0.Current == SignalStatus::Active)
		return ColorRGBA<unsigned char>(255, 50, 50, 255);
	else
		return ColorRGBA<unsigned char>(50, 255, 50, 255);
}

void ParticleSensorObserver::SaveIndividual(std::ofstream& Strm)
{
	Heat.Save(Strm);
	Signal0.Save(Strm);
	Signal1.Save(Strm);
}
void ParticleSensorObserver::LoadIndividual(std::ifstream& Strm)
{
	Heat.Load(Strm);
	Signal0.Load(Strm);
	Signal1.Load(Strm);
}


